<script setup lang="ts">
import { getCurrentInstance, ref, onMounted, nextTick } from 'vue'
const { proxy } = getCurrentInstance()

/* ---- */
const WXConfig = proxy.$Store.get('WXConfig') || {}

/* --设置微信分享功能-- */
proxy.$WxTool.loadJsdk(WXConfig, (obj) => {
  if (!obj) return
  // ----
  obj.hideMenuItem()
})

/* ---- */
const user = {
  company: '',
  telphone: '',
  address: ''
}
const pointImg = '/pics/center.gif'

/* ---- */
const layer = ref()

/* ---- */
const setAppTitle = (title) => {
  document.title = title
  // --利用iframe的onload事件刷新页面--
  const iframe = document.createElement('iframe')
  iframe.style.visibility = 'hidden'
  iframe.style.width = 0
  iframe.style.height = 0
  document.body.appendChild(iframe)
  nextTick(() => { document.body.removeChild(iframe) })
}
// ----
const setAddressMap = async () => {
  if (Number(user.address.length) <= 0) {
    proxy.$Myfun.forToast(layer.value, 'open', '请先设置详细地址！', () => { proxy.$router.back() })
    return false
  }
  // --腾讯地图展示门市地址--
  await proxy.$QQMap.loadQQmap({
    qqKey: proxy.$Global.qqKey,
    refObj: proxy.$refs.container,
    icon: pointImg,
    infoBox: '<div style="width: 100%;"><h1 style=" padding: 0 0 0.8rem; line-height: 2.5rem; font-size: 1.6rem; font-weight: bold; border-bottom: 1px solid #d7d7d7;">' + user.company + '</h1><div style=" padding: 0.8rem 0 0; line-height: 2.5rem; font-size: 1.4rem;">电话：' + user.telphone + '<br/>地址：' + user.address + '</div></div>',
    address: user.address
  }, '', '', true)
}
// ----
const getCurrencyInfo = async (callback) => {
  if (String(proxy.$route.query.fortable) === 'office') {
    const forOfficeData = { params: { 'OfficeSearch[id]': proxy.$route.query.dealer_id } }
    await proxy.$Axios.all([
      proxy.$Axios.get('office/index', forOfficeData)
    ]).then(proxy.$Axios.spread((officeData) => {
      const OData = officeData.items[0] || {}
      // ----
      user.company = proxy.$Myfun.setText(OData.name) || document.title
      user.telphone = OData.telphone
      // ----
      const forCity = OData.city_json.split(',')
      user.address = forCity[1] + forCity[2] + forCity[3] + proxy.$Myfun.setText(OData.address)
    })).catch((err) => { proxy.$Myfun.forToast(layer.value, 'open', err) })
  } else {
    const forUserData = { params: { 'DealerSearch[id]': proxy.$route.query.dealer_id } }
    await proxy.$Axios.all([
      proxy.$Axios.get('dealer/index', forUserData)
    ]).then(proxy.$Axios.spread((userData) => {
      const UData = userData.items[0] || {}
      // ----
      user.company = proxy.$Myfun.setText(UData.name)
      user.telphone = UData.tag || UData.telphone
      const forCitys = String(UData.province) === String(UData.city) ? UData.province + UData.district : UData.province + UData.city + UData.district
      user.address = Number(UData.address.indexOf(forCitys)) === Number(-1) ? forCitys + proxy.$Myfun.setText(UData.address) : proxy.$Myfun.setText(UData.address)
    })).catch((err) => { proxy.$Myfun.forToast(layer.value, 'open', err) })
  }
  // --设置网页title--
  const forTitle = user.company || document.title
  setAppTitle(forTitle)
  // ----
  callback && callback()
}

/* ---- */
onMounted(() => {
  getCurrencyInfo(() => {
    // --设置地图--
    setAddressMap()
    // ----
    setTimeout(() => { proxy.$Myfun.setSectionH() }, 300)
  })
})
</script>

<template>
  <div class="mescroll-touch">
    <div class="xw-section">
      <div class="mapBox" ref="container"></div>
    </div>
    <layer-view ref="layer"/>
  </div>
</template>

<style>
/* ---- */
.mapBox { height: 100%; overflow: visible; }
.mapBox div { overflow: visible; }
.mapBox table { width: auto; }
</style>
